package de.awg.accountanalyzer.io;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Locale;

import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
import org.supercsv.util.CsvContext;

public class ParseBetrag extends CellProcessorAdaptor {
	Locale myLocale = null;
	DecimalFormat df = null;

	/** important to invoke super */
	public ParseBetrag(String lPattern) {
		super();
		myLocale = Locale.getDefault();
		df = (DecimalFormat) DecimalFormat.getInstance(myLocale);
		// df.applyPattern("#,##0.00;-#,##0.00");
		df.applyPattern(lPattern);
	}

	/** important to invoke super */
	public ParseBetrag(final DoubleCellProcessor next) {
		super(next);
	}

//	/** simplify conversion of column to Long */
	@Override
	public Object execute(Object value, CsvContext context) {
		Double result = null;
		try {
			result = df.parse((String) value).doubleValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return next.execute(result, context);
	}
	
}
